/***************************************************************
 * Name:      main.cpp
 * Purpose:   Open Asset to local data structure
 * Author:    Riku Maikola (riku@maikola.net)
 * Usage:     OpenDXF "out directory" "*.dxf"
 * Created:   2014-05-26
 * Copyright: Riku Maikola
 * License:   GPL
 **************************************************************/

#include <mPoint.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <sstream>

#include "dl_dxf.h"
#include "dl_creationadapter.h"
#include "Elements.h"

using namespace std;

//****************//
//** CORE CLASS **//
//****************//
class mLoad : public DL_CreationAdapter
{
private:
    string home;

    virtual void addBlock(const DL_BlockData& data);
    virtual void addLayer(const DL_LayerData& data);
    virtual void addPoint(const DL_PointData& data);
    virtual void addLine(const DL_LineData& data);
    virtual void addArc(const DL_ArcData& data);
    virtual void addCircle(const DL_CircleData& data);

public:
    element P;
    vector<element> ALL;
    mLoad(string,string);
    bool SaveElement();
};

//***********************************//
//**                               **//
//***********************************//
mLoad::mLoad(string ho,string filename)
{
    home = ho;
    if(filename.rfind(".dxf"))
    {
        DL_Dxf* dxf = new DL_Dxf();
        if (!dxf->in( filename,this))return;
        delete dxf;

        SaveElement();
        return;
    }
    return;
}


//***********************************//
//**                               **//
//***********************************//
void mLoad::addBlock(const DL_BlockData& data)
{
    //printf("BLOCK %s X%f  Y%6.3f Z%6.3f\n", data.name.c_str() , data.bpx , data.bpy , data.bpz);
    //wxString L(data.name.c_str(), wxConvUTF8);
    //addLayer(L,data.flags,attributes.getColor());
}
//***********************************//
//**                               **//
//***********************************//
void mLoad::addLayer(const DL_LayerData& data)
{
    //wxString name(data.name.c_str(), wxConvUTF8);
    //L.Add(name);
}

//*************************************//
//*****                              **//
//*************************************//
void mLoad::addPoint(const DL_PointData& data)
{
    //wxString L(attributes.getLayer().c_str(), wxConvUTF8);
    //addPoint(data.x,data.y,L);
}
//*******************


void mLoad::addArc(const DL_ArcData& data)
{
    P.T = 3;
    P.p1.SetValue(mP(data.cx,data.cy,data.cz).GetFromPoint(data.angle1*DegToRad,data.radius));
    P.p2.SetValue(data.cx,data.cy,data.cz);
    P.p3.SetValue(mP(data.cx,data.cy,data.cz).GetFromPoint(data.angle2*DegToRad,data.radius));
    ALL.push_back(P);
};

void mLoad::addCircle(const DL_CircleData& data)
{
    P.T = 4;
    P.p1.SetValue(mP(data.cx,data.cy,data.cz).GetFromPoint(0,data.radius));
    P.p2.SetValue(data.cx,data.cy,data.cz);
    P.p3.SetValue(mP(data.cx,data.cy,data.cz).GetFromPoint(TAU,data.radius));
    ALL.push_back(P);
}


void mLoad::addLine(const DL_LineData& data)
{
    P.T = 1;
    P.p1.SetValue(data.x1,data.y1,data.z1);
    P.p2.SetValue(NAN,NAN,NAN);
    P.p3.SetValue(data.x2,data.y2,data.z2);
     ALL.push_back(P);
};

bool mLoad::SaveElement()
{
    ofstream myfile;
    streampos size;
    myfile.open ( home.c_str(),ios::out | ios::trunc | ios::binary);
    if(myfile.is_open())
    {
        myfile.write( (char*)ALL.data() , sizeof(element)*ALL.size() );
        myfile.close();
    }
    return true;
};



int main(int n, char* Args[])
{
    if(n==3)
    {
        mLoad mLoad(Args[1],Args[2]);
    }
    return 0;
}


